package com.oceansoft.core.common.util.impexp4xls;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.oceansoft.core.common.util.StringUtil;
import com.oceansoft.core.common.util.impexp4xls.help.BuildRecordSQLHelper;
import com.oceansoft.core.common.util.impexp4xls.help.ConfigFileLoadHelper;
import com.oceansoft.core.common.util.impexp4xls.help.GetXLSHelper;

public class POIImporterImpl implements XLSImporter {

	// 全局配置
	HashMap<String, Object> configure = new HashMap<String, Object>();

	@SuppressWarnings("rawtypes")
	public void loadConfig(String configName, Map extraMap) {
		String configPath = ConfigFileLoadHelper.getConfigPath(configName);
		ConfigFileLoadHelper.loadCofingFile(configPath, configure, extraMap);
	}

	public void setConfig(Map<String, Object> configMap) {
		this.configure = (HashMap<String, Object>) configMap;
	}

	@SuppressWarnings("rawtypes")
	public List<?> getXLSData(String filePath) {
		List<?> returnList = new ArrayList();
		Iterator<Row> rowit = null;
		if (StringUtil.isNotNull(filePath)) {
			String fileExtName = filePath.split("[.]")[filePath.split("[.]").length - 1];
			try {
				if ("xls".equalsIgnoreCase(fileExtName)) {
					// 2007之前格式
					HSSFWorkbook book = new HSSFWorkbook(new FileInputStream(new File(filePath)));
					HSSFSheet sheet = book.getSheetAt(0);
					rowit = sheet.iterator();
				} else if ("xlsx".equalsIgnoreCase(fileExtName)) {
					XSSFWorkbook book = new XSSFWorkbook(new FileInputStream(new File(filePath)));
					XSSFSheet sheet = book.getSheetAt(0);
					rowit = sheet.iterator();
				}

				if (rowit != null) {
					returnList = GetXLSHelper.transferXLSData(configure, rowit);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			throw new RuntimeException("数据不存在");
		}
		return returnList;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void save2DB(List<?> list) {
		if (list != null && !list.isEmpty()) {
			String importMode = (String) ((Map) configure.get("customProperty")).get("importMode");
			List fieldNameList = (List) configure.get("fieldNameList");
			String errorIndex = "";
			for (int i = 0; i < list.size(); i++) {
				String sql = "";
				Map row = (Map) list.get(i);
				if ("insert".equals(importMode)) {
					sql = BuildRecordSQLHelper.buildInsertSQL((Map) configure.get("customProperty"),
							fieldNameList, row);
				}
				if (StringUtil.isNotNull(sql)) {
				}
			}
			System.out.println("数据出错行:" + errorIndex);
		}
	}

	@SuppressWarnings("rawtypes")
	public void importXLS(String configfile, List<?> record, String filePath, Map extraMap) {
		loadConfig(configfile, extraMap);
		if (record == null || record.isEmpty()) {
			record = getXLSData(filePath);
		}
		save2DB(record);
	}
}
